<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="字典类别名称" v-model="listQuery.name"> </el-input>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item"  v-if="dictManager_btn_add"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="序号" min-width='30%' >
        <template scope="scope">
          <span>{{scope.$index + 1}} </span>
				</template>
				</el-table-column>
				<el-table-column  align="center" label="字典类别名称"><template scope="scope">
				  <span>
				    {{scope.row.name}}</span>
				</template>
				</el-table-column>
				<el-table-column  align="center" label="类别标识"><template scope="scope">
				  <span>
				    {{scope.row.typeId}}</span>
				</template>
				</el-table-column>
				<el-table-column  align="center" label="详细描述" min-width="140%"><template scope="scope">
				  <span>
				    {{scope.row.description}}</span>
				</template>
				</el-table-column>
				<el-table-column  align="center" label="启用状态"><template scope="scope">
				  <span v-if="scope.row.status==1" class="el-tag--success">
		        <!--{{scope.row.status==1?'已启用' : '已停用'}}-->已启用
				  </span>
				   <span v-if="scope.row.status==0" class="el-tag--danger">
		        <!--{{scope.row.status==1?'已启用' : '已停用'}}-->已停用
				  </span>
				    <el-button  size="small" type="danger"
                   @click="HandleChangeStatus(scope.row)" v-if="scope.row.status==1 && dictManager_btn_change">停用
						</el-button>
						<el-button  size="small" type="success"
                   @click="HandleChangeStatus(scope.row)" v-if="scope.row.status==0 && dictManager_btn_change">启用
						</el-button>
				</template>
				</el-table-column>
				</el-table-column>
				<el-table-column align="center" label="操作" min-width="140%"> <template scope="scope">
		        <el-button v-if="dictManager_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
		        </el-button>
		        <!--<el-button v-if="dictManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
		        </el-button>-->
					 	<el-button v-if="dictManager_btn_detail" size="small" type="warning" @click="handlerDetail(scope.row)">明细
						</el-button>
		      </template>
				</el-table-column>
        </el-table>
        <div v-show="!listLoading" class="pagination-container">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
        </div>
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
			    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
			      <el-form-item label="字典类别" prop="name">
			        <el-input v-model="form.name" placeholder="请输入字典类别名称"></el-input>
			      </el-form-item>
			      <el-form-item  label="类别标识" prop="typeId">
			        <el-input  v-if="dialogStatus=='create'"  v-model ="form.typeId" placeholder="请输入字典类别标识"></el-input>
			        <el-input  v-else v-model ="form.typeId" disabled placeholder="请输入字典类别标识"></el-input>
			      </el-form-item>
			      <el-form-item label="启用状态">
			        <el-select class="filter-item" v-model="form.status" placeholder="请选择">
			          <el-option v-for="item in  statusOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
			        </el-select>
			      </el-form-item>
			      <el-form-item label="描述">
			        <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 5}" placeholder="请输入内容" v-model="form.description"> </el-input>
			      </el-form-item>
			    </el-form>
			    <div slot="footer" class="dialog-footer">
			      <el-button @click="cancel('form')">取 消</el-button>
			      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
			      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
			    </div>
			  </el-dialog>

			  <el-dialog  style="top: -14%;min-height:600px;" :modal-append-to-body="false" ref="dictDetail"  :title="dialogDetailName" :visible.sync="dialogDetailVisible" custom-class="el-dialog el-dialog--large dialog-custom" >
			    <dict-detail :id="currentId"  @closeDetailDialog="closeDetailDialog"></dict-detail>
			  </el-dialog>
    </div>
</template>

<script>
import {
  page,
  addObj,
  getObj,
  putObj,
  changeStatus
} from 'api/admin/dict/index';
import dictDataPane from './components/dictDetail';
import { mapGetters } from 'vuex';
export default {
  name: 'dictManager',
  components: {
    'dict-detail': dictDataPane,
  },
  data() {
    return {
    	form: {
        name: undefined,
        typeId:undefined,
        status: '启用',
        description: undefined
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入字典类别',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        typeId: [
	        {
            required: true,
            message: '请输入字典类别标识',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到10 个字符',
            trigger: 'blur'
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined
      },
      statusOptions: [
      {
        value: '1',
        label: '启用'
      },
      {
        value: '0',
        label: '停用'
      }
      ],
      dialogFormVisible: false,
      dialogStatus: '',
      dictManager_btn_edit: false,
      dictManager_btn_add: false,
      dictManager_btn_detail: true,
      dialogDetailName: '字典数据列表',
      dialogDetailVisible: false,
      textMap: {
        update: '编辑',
        create: '创建',
        detail: '明细',
      },
      currentId: -1,
      tableKey: 0
    }
  },
  created() {
    this.getList();
    //權限控制的按鈕是否顯示
    this.dictManager_btn_edit = this.elements['dictManager:btn_edit'];
    this.dictManager_btn_change = this.elements['dictManager:btn_change'];
    this.dictManager_btn_add = this.elements['dictManager:btn_add'];
    //this.dictManager_btn_detail = this.elements['dictManager:btn_detail'];
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
  	//获取字典type列表
    getList() {
      this.listLoading = true;
      page(this.listQuery).then(response => {
        this.list = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      })
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    //获取详细信息
    handleUpdate(row) {
      getObj(row.id)
        .then(response => {
          this.form = response.data;
          this.dialogFormVisible = true;
          this.dialogStatus = 'update';
        });
    },
    handlerDetail(row) {
      this.dialogDetailVisible = true;
      this.currentId = row.typeId;
       if (this.$refs.dictDetail !== undefined) {
        this.$refs.dictDetail.id = this.currentId;
      }
      this.$store.state.dictTypeId = this.currentId;
    },
    //更改 启用/停用 状态
    HandleChangeStatus(row){
    	const status = row.status;
    	let toStatus="";
    	let alertString="";
    	if(status==1){
    		alertString = "设置为停用状态";
    		toStatus = 0;
    	}else{
    		alertString = "设置为启用状态";
    		toStatus = 1;
    	}
    	let param = {};
    	param.id = row.id;
    	param.status = toStatus;
    	this.$confirm('是否'+alertString+', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          changeStatus(param)
            .then(() => {
              this.$notify({
                title: '成功',
                message: '设置成功',
                type: 'success',
                duration: 2000
              });
              const index = this.list.indexOf(row);
             	this.list[index].status = toStatus;
            });
        });
    },
    //添加
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form)
            .then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
            })
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    //修改信息
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          putObj(this.form.id, this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    resetTemp() {
      this.form = {
        name: undefined,
        status: '1',
        description: undefined
      };
    },
    closeDetailDialog() {
      this.dialogDetailVisible = false;
    }
  }
}
</script>
<style>
.dialog-custom {
  min-height:690px;

}
</style>

